package com.csw.flink.core

import org.apache.flink.streaming.api.scala._

object Demo01WordCount {

  def main(args: Array[String]): Unit = {

    /**
      * 创建flink环境
      *
      */

    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment


    /**
      * 设置并行为1，  默认服务器有几核就有几个
      */

    env.setParallelism(1)

    /**
      *
      * 读取数据
      *
      * 读取socket  数据
      *
      *
      * nc -lk  8888
      */


    val linesDS: DataStream[String] = env.socketTextStream("master", 8888)


    /**
      * 数据处理
      *
      */

    val wordsDS: DataStream[String] = linesDS.flatMap(i => i.split(","))

    val kvDS: DataStream[(String, Int)] = wordsDS.map(word => (word, 1))

    //按照单词进行分组(将同一个单词分到同一个流中)
    val keyByDS: KeyedStream[(String, Int), String] = kvDS.keyBy(kv => kv._1)

    /**
      * flink 的算子本身就是有状态的算子
      *
      */

    //对列进行求和
    val countDS: DataStream[(String, Int)] = keyByDS.sum(1)

    /**
      * 打印数据
      *
      */
    countDS.print()


    /**
      * 启动flink程序
      */
    env.execute()
  }
}
